package com.blast.apply.presenter

import android.content.Intent
import android.view.KeyEvent
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.blast.apply.adapter.UnApplyAdapter
import com.blast.apply.bean.AuthBean
import com.blast.apply.ui.UnAuthDetailActivity
import com.wxl.common.base.AbsActivity
import com.wxl.common.life.LifecycleManager
import com.wxl.common.room.entity.UnApplyBlastEntity
import com.wxl.common.room.helper.DBHelper
import com.wxl.common.util.LayoutHelper
import com.wxl.common.util.ObservableUtil
import com.wxl.common.viewmodel.AbsPresenter
import kotlinx.android.synthetic.main.activity_apply_list_layout.view.*

class UnApplyListPresenter : AbsPresenter() {

    lateinit var rootView: View
    private var mUnApplyAdapter: UnApplyAdapter? = null

    override fun onCreated(vararg args: Any) {
        rootView = args[0] as View
        val ab = AuthBean()
        ab.observe((rootView.context as AbsActivity), Observer {
            if(it!!.isAuth == 1) {
                queryAuthData()
            }
        })
        LifecycleManager.manager.registerLiveData(ab,(rootView.context as AbsActivity).lifecycle)
        queryAuthData()
    }


    private fun queryAuthData(){
        DBHelper.queryAppledBlastData(object :
            ObservableUtil.Companion.OnNextCallback<List<UnApplyBlastEntity>>() {
            override fun onNext(t: List<UnApplyBlastEntity>) {
                if (t.isNullOrEmpty()) {
                    rootView.applyListEmptyView.visibility = View.VISIBLE
                    rootView.applyListSmartView.visibility = View.GONE
                } else {
                    rootView.applyListEmptyView.visibility = View.GONE
                    rootView.applyListSmartView.visibility = View.VISIBLE
                }
                if(mUnApplyAdapter == null) {
                    mUnApplyAdapter = UnApplyAdapter(t)
                    rootView.applyListView.layoutManager = LinearLayoutManager(rootView.context)
                    LayoutHelper.getDetonatorTitleViewsWidth(rootView.applyListTitleLayout,object:ObservableUtil.Companion.OnNextCallback<ArrayList<Int>>(){
                        override fun onNext(t: ArrayList<Int>) {
                            mUnApplyAdapter?.setWidths(t)
                            rootView.applyListView.adapter = mUnApplyAdapter
                            mUnApplyAdapter?.bindToRecyclerView(rootView.applyListView)
                            mUnApplyAdapter?.setOnItemClickListener { adapter, view, position ->
                                lookAuthDetail(position)
                            }
                        }
                    })

                } else {
                    mUnApplyAdapter!!.setNewData(t)
                }
            }
        })
    }


    /**
     * 查看
     */
    private fun lookAuthDetail(position:Int){
        mUnApplyAdapter?.let {
            val intent = Intent(rootView.context,UnAuthDetailActivity::class.java)
            val lgs = it.data[position].lgs.lg as ArrayList<UnApplyBlastEntity.LgsBean.LgBean>
            intent.putExtra("lgs",lgs)
            it.data[position].jbqys?.apply {
                if(!jbqy.isNullOrEmpty()){
                    val lanlat = jbqy[0].jbqyjd+","+jbqy[0].jbqywd
                    intent.putExtra("lanlat",lanlat)
                }
            }
            intent.putExtra("position",position)
            rootView.context.startActivity(intent)
        }
    }

    fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        mUnApplyAdapter?.onKeyDown(keyCode, event)
        if( keyCode == KeyEvent.KEYCODE_ENTER ||  keyCode == KeyEvent.KEYCODE_F1){
            mUnApplyAdapter?.let {
                if(it.getSelectedPosition() >= 0){
                    lookAuthDetail(it.getSelectedPosition())
                }
            }
        }
        return false
    }


}